home *** CD-ROM | disk | FTP | other *** search
/ Delphi 2.0 - Programmer's Utilities Power Pack / Delphi 2.0 Programmer's Utilities Power Pack.iso / a_to_d / delftips / ti2825.asc < prev    next >
Text File  |  1996-09-15  |  10KB  |  305 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.   PRODUCT  :  Delphi                                 NUMBER  :  2825
  8.   VERSION  :  All
  9.        OS  :  Windows
  10.      DATE  :  August 17, 1995                          PAGE  :  1/5
  11.  
  12.     TITLE  :  Extracting Index Data From A Table
  13.  
  14.  
  15.  
  16.  
  17. Getting a list of the indexes associated with a table at run-time can be
  18. as simple as a call to the GetIndexNames method of the TTable, TQuery, or
  19. TStoredProc component. The GetIndexNames method returns a list of the
  20. that are available for the data set in the form of a TStrings list, which
  21. may then be inserted into such visual components as a TListBox through its
  22. Items property:
  23.  
  24.   ListBox1.Clear;
  25.   Table1.GetIndexNames(ListBox1.Items);
  26.   
  27. Of course, the TStrings list returned by the GetIndexNames method need not
  28. be used with a visual component. It could just as well serve as an array
  29. of index names stored entirely in memory, that can be used as a list or
  30. array.
  31.   
  32. But it is also possible to retrieve much more information about the
  33. indexes for a table than just the names. Other descriptive attributes
  34. include the name of each index, the names of the fields that comprise each
  35. index, and the index options used when each index was created. Retrieving
  36. these values is slightly more involved than the use of the GetIndexNames.
  37. Basically, this process involves iterating through the IndexDefs property
  38. of the TTable, TQuery, or TStoredProc component. The IndexDefs property is
  39. essentially an array of records, one record for each index for the table.
  40. Each index record contains information about the index. It is a relatively
  41. straightforward process to iterate through this array of index descrip-
  42. tions, extracting information about individual indexes.
  43.  
  44. The IndexDefs property of the TTable component contains information about
  45. the indexes for the table used by the TTable, TQuery, or TStoredProc comp-
  46. onent in use. The IndexDefs property itself has various properties that
  47. allow for the extraction of information about specific indexes. The two
  48. properties in the IndexDefs object are:
  49.  
  50.   Count: type Integer; available only at run-time and read-only; indicates
  51.          the number ofentries in the Items property (i.e., the number of
  52.          indexes in the table).
  53.   Items: type TIndexDef; available only at run-time and read-only; an
  54.          array of TIndexDef objects, one for each index in the table.
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.   PRODUCT  :  Delphi                                 NUMBER  :  2825
  69.   VERSION  :  All
  70.        OS  :  Windows
  71.      DATE  :  August 17, 1995                          PAGE  :  2/5
  72.  
  73.     TITLE  :  Extracting Index Data From A Table
  74.  
  75.  
  76.  
  77.  
  78.          
  79. The Count property of the IndexDefs object is used as the basis for a
  80. loop program construct to iterate through the Items property entries to
  81. extract specific information about each index. Each IndexDef object con-
  82. tained in the Items property consists of a number of properties that pro-
  83. vide various bits of information that describe each index. All of the
  84. properties of the IndexDef object are available only at run-time and are
  85. all read-only. These properties are:
  86.  
  87.   Expression: type String; indicates the expression used for dBASE multi-
  88.               field indexes.
  89.   Fields:     type String; indicates the field or fields upon which the
  90.               index is based.
  91.   Name:       type String; name of the index.
  92.   Options:    type TIndexOptions; characteristics of the index (ixPrimary,
  93.               ixUnique, etc.).
  94.  
  95. Before any index information (Count or Items) can be accessed, the Update
  96. method of the IndexDefs object must be called. This refreshes or init-
  97. ializes the IndexDef object's view of the set of indexes.
  98.  
  99. Examples
  100. ========
  101.  
  102. Here is a simple For loop based on the Count property of the IndexDefs
  103. object that extracts the name of each index (if any exist) for the table
  104. represented by the TTable component Table1:
  105.  
  106.   procedure TForm1.ListBtnClick(Sender: TObject);
  107.   var
  108.     i: Integer;
  109.   begin
  110.     ListBox1.Items.Clear;
  111.     with Table1 do begin
  112.       if IndexDefs.Count > 0 then begin
  113.         for i := 0 to IndexDefs.Count - 1 do
  114.           ListBox1.Items.Add(IndexDefs.Items[i].Name)
  115.       end;
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.   PRODUCT  :  Delphi                                 NUMBER  :  2825
  130.   VERSION  :  All
  131.        OS  :  Windows
  132.      DATE  :  August 17, 1995                          PAGE  :  3/5
  133.  
  134.     TITLE  :  Extracting Index Data From A Table
  135.  
  136.  
  137.  
  138.  
  139.     end;
  140.   end;
  141.  
  142. Below is an example showing how to extract information about indexes at
  143. run-time, plugging the extracted values into a TStringGrid (named SG1).
  144.  
  145.   procedure TForm1.FormShow(Sender: TObject);
  146.   var
  147.     i: Integer;
  148.     S: String;
  149.   begin
  150.     with Table1 do begin
  151.       Open;
  152.       {Refresh IndexDefs object}
  153.       IndexDefs.Update;
  154.       if IndexDefs.Count > 0 then begin
  155.         {Set up columns and rows in grid to match IndexDefs items}
  156.         SG1.ColCount := 4;
  157.         SG1.RowCount := IndexDefs.Count + 1;
  158.         {Set grid column labels to TIndexDef property names}
  159.         SG1.Cells[0, 0] := 'Name';
  160.         SG1.ColWidths[0] := 200;
  161.         SG1.Cells[1, 0] := 'Fields';
  162.         SG1.ColWidths[1] := 200;
  163.         SG1.Cells[2, 0] := 'Expression';
  164.         SG1.ColWidths[2] := 200;
  165.         SG1.Cells[3, 0] := 'Options';
  166.         SG1.ColWidths[3] := 300;
  167.         {Loop through IndexDefs.Items}
  168.         for i := 0 to IndexDefs.Count - 1 do begin
  169.           {Fill grid cells for current row}
  170.           SG1.Cells[0, i + 1] := IndexDefs.Items[i].Name;
  171.           SG1.Cells[1, i + 1] := IndexDefs.Items[i].Fields;
  172.           SG1.Cells[2, i + 1] := IndexDefs.Items[i].Expression;
  173.           if ixPrimary in IndexDefs.Items[i].Options then
  174.             S := 'ixPrimary, ';
  175.           if ixUnique in IndexDefs.Items[i].Options then
  176.             S := S + 'ixUnique, ';
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.   PRODUCT  :  Delphi                                 NUMBER  :  2825
  191.   VERSION  :  All
  192.        OS  :  Windows
  193.      DATE  :  August 17, 1995                          PAGE  :  4/5
  194.  
  195.     TITLE  :  Extracting Index Data From A Table
  196.  
  197.  
  198.  
  199.  
  200.           if ixDescending in IndexDefs.Items[i].Options then
  201.             S := S + 'ixDescending, ';
  202.           if ixCaseInsensitive in IndexDefs.Items[i].Options then
  203.             S := S + 'ixCaseInsensitive, ';
  204.           if ixExpression in IndexDefs.Items[i].Options then
  205.             S := S + 'ixExpression, ';
  206.           if S > ' ' then begin
  207.             {Get rid of trailing ", "}
  208.             System.Delete(S, Length(S) - 1, 2);
  209.             SG1.Cells[3, i + 1] := S;
  210.           end;
  211.         end;
  212.       end;
  213.     end;
  214.   end;
  215.  
  216. Special Considerations
  217. ======================
  218.  
  219. There are idiosyncracies associated with extracting information about
  220. indexes for different table types that Delphi can access.
  221.  
  222. dBASE Tables
  223. ------------
  224.  
  225. With dBASE indexes, which properties of Fields and Expression will be
  226. filled will depend on the type of index, simple (single-field) or
  227. complex (based on multiple fields or a dBASE expression). If the index
  228. is a simple one, the Fields property will contain the name of the field
  229. in the table on which the index is based and the Expression property will
  230. be blank. If the index is a complex one, the Expression property will
  231. show the expression on which the index is based (e.g., "Field1+Field2")
  232. and the Fields property will be blank.
  233.  
  234. Paradox Tables
  235. --------------
  236.  
  237. With Paradox primary indexes, the Name property will be blank, the Fields
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.   PRODUCT  :  Delphi                                 NUMBER  :  2825
  252.   VERSION  :  All
  253.        OS  :  Windows
  254.      DATE  :  August 17, 1995                          PAGE  :  5/5
  255.  
  256.     TITLE  :  Extracting Index Data From A Table
  257.  
  258.  
  259.  
  260.  
  261. property will contain the field(s) on which the index is based, and the
  262. Options property will contain ixPrimary. With secondary indexes, the Name
  263. property will contain the name of the secondary index, the Fields prop-
  264. erty will contain the field(s) on which the index is based, and the
  265. Options property may or may